<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="160px">
      <el-row>
        <el-col :span="6">
      <el-form-item label="id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :span="6">
      <el-form-item label="比赛id" prop="race">
        <el-input
          v-model="queryParams.race"
          placeholder="请输入比赛id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="6">
      <el-form-item label="比赛负责人姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入比赛负责人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :span="6">
      <el-form-item label="比赛负责人手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入比赛负责人手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :span="6">
      <el-form-item label="比赛负责人邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入比赛负责人邮箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="6">
      <el-form-item label="最大可报名人数" prop="capacity">
        <el-input
          v-model="queryParams.capacity"
          placeholder="请输入最大可报名人数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :span="6">
      <el-form-item label="推荐排名" prop="rRank">
        <el-input
          v-model="queryParams.rRank"
          placeholder="请输入推荐排名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :span="6">
      <el-form-item label="限制老师最大指导数量" prop="tLimit" style="width: 365px">
        <el-input
          v-model="queryParams.tLimit"
          placeholder="请输入限制老师最大指导数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
      </el-row>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['buis:raceDetail:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['buis:raceDetail:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['buis:raceDetail:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['buis:raceDetail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="raceDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="比赛名称" align="center" prop="raceName" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="比赛id" align="center" prop="race" />
      <el-table-column label="比赛负责人姓名" align="center" prop="name" />
      <el-table-column label="比赛负责人手机号" align="center" prop="phone" />
      <el-table-column label="比赛负责人邮箱" align="center" prop="email" />
      <el-table-column label="最大可报名人数" align="center" prop="capacity" />
      <el-table-column label="推荐排名" align="center" prop="rRank" />
      <el-table-column label="限制老师最大指导数量" align="center" prop="tLimit" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['buis:raceDetail:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['buis:raceDetail:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改比赛详情对话框 -->
    <el-dialog :title="title" v-model="open" width="1300px" append-to-body>
      <el-form ref="raceDetailRef" :model="form" :rules="rules" label-width="170px" >
        <el-row :gutter="20">
          <el-col :span="12" >
        <el-form-item label="比赛id" prop="race">
          <el-input v-model="form.race" placeholder="请输入比赛id" style="width: 300px"/>
        </el-form-item>
        <el-form-item label="比赛负责人姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入比赛负责人姓名" style="width: 300px"/>
        </el-form-item>
        <el-form-item label="比赛负责人手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入比赛负责人手机号"style="width: 300px" />
        </el-form-item>
        <el-form-item label="比赛负责人邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入比赛负责人邮箱" style="width: 300px"/>
        </el-form-item>
        <el-form-item label="比赛说明材料" prop="descUrl">
          <file-upload v-model="form.descUrl"/>
        </el-form-item>
        <el-form-item label="比赛群二维码" prop="groupUrl">
          <image-upload v-model="form.groupUrl"/>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="计分规则" prop="rule">
          <el-select v-model="form.rule" placeholder="请选择计分规则"style="width: 300px">
            <el-option
              v-for="dict in score_rule"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最大可报名人数" prop="capacity">
          <el-input v-model="form.capacity" placeholder="请输入最大可报名人数" style="width: 300px"/>
        </el-form-item>
        <el-form-item label="队伍最小人数" prop="teamMin">
          <el-input v-model="form.teamMin" placeholder="请输入队伍最小人数"style="width: 300px" />
        </el-form-item>
        <el-form-item label="队伍最大人数" prop="teamMax">
          <el-input v-model="form.teamMax" placeholder="请输入队伍最大人数" style="width: 300px"/>
        </el-form-item>
        <el-form-item label="老师最小人数" prop="teacherMin">
          <el-input v-model="form.teacherMin" placeholder="请输入老师最小人数" style="width: 300px"/>
        </el-form-item>
        <el-form-item label="老师最大人数" prop="teacherMax">
          <el-input v-model="form.teacherMax" placeholder="请输入老师最大人数" style="width: 300px"/>
        </el-form-item>
        <el-form-item label="推荐排名" prop="rRank">
          <el-input v-model="form.rRank" placeholder="请输入推荐排名" style="width: 300px"/>
        </el-form-item>
        <el-form-item label="限制老师最大指导数量" prop="tLimit">
          <el-input v-model.number="form.tLimit" placeholder="请输入限制老师最大指导数量" style="width: 300px"/>
        </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">分数权重信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddWeight">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteWeight">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="weightList" :row-class-name="rowWeightIndex" @selection-change="handleWeightSelectionChange" ref="weight">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="170"/>
          <el-table-column label="模块名称" prop="name" width="420">
            <template #default="scope">
              <el-input v-model="scope.row.name" placeholder="请输入模块名称" />
            </template>
          </el-table-column>
          <el-table-column label="权重占比" prop="weight" width="630">
            <template #default="scope">
              <el-input v-model="scope.row.weight" placeholder="请输入权重占比" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RaceDetail">
import { listRaceDetail, getRaceDetail, delRaceDetail, addRaceDetail, updateRaceDetail } from "@/api/buis/raceDetail";

const { proxy } = getCurrentInstance();
const { score_rule } = proxy.useDict('score_rule');

const raceDetailList = ref([]);
const weightList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedWeight = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    race: null,
    name: null,
    phone: null,
    email: null,
    capacity: null,
    rRank: null,
    tLimit: null,
  },
  rules: {
    race: [
      { required: true, message: "比赛(关联比赛表id)不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "比赛负责人姓名不能为空", trigger: "blur" }
    ],
    phone: [
      { required: true, message: "比赛负责人手机号不能为空", trigger: "blur" }
    ],
    email: [
      { required: true, message: "比赛负责人邮箱不能为空", trigger: "blur" }
    ],
    descUrl: [
      { required: true, message: "比赛说明材料不能为空", trigger: "blur" }
    ],
    groupUrl: [
      { required: true, message: "比赛群二维码不能为空", trigger: "blur" }
    ],
    rule: [
      { required: true, message: "计分规则不能为空", trigger: "change" }
    ],
    capacity: [
      { required: true, message: "最大可报名人数不能为空", trigger: "blur" }
    ],
    teamMin: [
      { required: true, message: "队伍最小人数不能为空", trigger: "blur" }
    ],
    teamMax: [
      { required: true, message: "队伍最大人数不能为空", trigger: "blur" }
    ],
    teacherMin: [
      { required: true, message: "老师最小人数不能为空", trigger: "blur" }
    ],
    teacherMax: [
      { required: true, message: "老师最大人数不能为空", trigger: "blur" }
    ],
    tLimit: [
      { required: true, message: "限制老师最大指导数量不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询比赛详情列表 */
function getList() {
  loading.value = true;
  listRaceDetail(queryParams.value).then(response => {
    raceDetailList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    race: null,
    name: null,
    phone: null,
    email: null,
    descUrl: null,
    groupUrl: null,
    rule: null,
    capacity: null,
    teamMin: null,
    teamMax: null,
    teacherMin: null,
    teacherMax: null,
    rRank: null,
    tLimit: null,
    isDeleted: null,
    createTime: null,
    updateTime: null
  };
  weightList.value = [];
  proxy.resetForm("raceDetailRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加比赛详情";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getRaceDetail(_id).then(response => {
    form.value = response.data;
    weightList.value = response.data.weightList;
    open.value = true;
    title.value = "修改比赛详情";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["raceDetailRef"].validate(valid => {
    if (valid) {
      form.value.weightList = weightList.value;
      if (form.value.id != null) {
        updateRaceDetail(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRaceDetail(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除比赛详情编号为"' + _ids + '"的数据项？').then(function() {
    return delRaceDetail(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 分数权重序号 */
function rowWeightIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 分数权重添加按钮操作 */
function handleAddWeight() {
  let obj = {};
  obj.name = "";
  obj.weight = "";
  weightList.value.push(obj);
}

/** 分数权重删除按钮操作 */
function handleDeleteWeight() {
  if (checkedWeight.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的分数权重数据");
  } else {
    const weights = weightList.value;
    const checkedWeights = checkedWeight.value;
    weightList.value = weights.filter(function(item) {
      return checkedWeights.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleWeightSelectionChange(selection) {
  checkedWeight.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('buis/raceDetail/export', {
    ...queryParams.value
  }, `raceDetail_${new Date().getTime()}.xlsx`)
}

getList();
</script>
